package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.Currency;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * implementation of GenericDAOInterface
 * @author Enrico Baroni
 *
 */
public class CurrencyDAO implements GenericInterfaceDAO {

	@Autowired
	 private SessionFactory sessionFactory;
	

	/**
	 * default constructor
	 */
	public CurrencyDAO () {
		
	}
	
	
	
	/* 
	 * selects one currency by Id
	 * @param id
	 * @return currency
	 */
	 @Transactional(readOnly = true)
	public Currency selectById(int id) {
		 Currency currency = (Currency) getSession().get(Currency.class, id);
	    return currency;
	}

	/*
	 * retrieves all currencys from db
	 * @return List of currencys
	 */
	 @Transactional(readOnly = true)
	public List<Currency> selectAll() {
	 
	    List<Currency> currencys = getSession().createQuery("from currency").list();
	    return currencys;
	}

	/*
	 * inserts a new currency in database
	 * retrieves generated id and sets to currency instance
	 * @param new currency
	 */
	 @Transactional
	public void insert(Currency currency) {
	    Integer id = (Integer) getSession().save(currency);
	    currency.setId(id);           
	}

	/*
	 * updates currency
	 * @param currency to update
	 */
	 @Transactional
	public void update(Currency currency) {
		getSession().merge(currency);
	}

	/*
	 * delete given currency
	 * @param currency to delete
	 */
	 @Transactional
	public void delete(Currency currency) {
		getSession().delete(currency);
	}



	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void insert(Object newInsert) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void update(Object future) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public Session getSession() {
		 return sessionFactory.getCurrentSession();
	}
}
